<?php

class DigitalmapAction extends PublicAction
{
    protected $digitalmapModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->digitalmapModel = D('Digitalmap');
    }

    //列表
    public function index()
    {
        $map = array();
        //取得满足条件的记录总数
        $count = $this->digitalmapModel->where($map)->count('id');

        if ($count > 0) {
            import("@.ORG.Page");
            $listRows = !empty($_REQUEST['listRows']) ? $_REQUEST['listRows'] : C('PAGE_LISTROWS');
            //创建分页对象
            $p = new Page($count, $listRows);

            //分页查询数据
            $voList = $this->digitalmapModel->where($map)->limit($p->firstRow . ',' . $p->listRows)->select();

            //分页显示
            $page = $p->show();

            //模板赋值显示
            $this->assign('list', $voList);
            $this->assign('page', $page);
        }

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    //百度地图
    public function edit()
    {
        if (IS_POST) {
            $data = $_POST;
            $baidumap['bdmap_x'] = $_POST['bdmap_x'];
            $baidumap['bdmap_y'] = $_POST['bdmap_y'];
            $baidumap['name'] = $_POST['name'];
            $baidumap['address'] = $_POST['address'];
            $baidumap['tel'] = $_POST['tel'];
            $data['config'] = json_encode($baidumap);
            if (false === $this->digitalmapModel->create()) {
                $this->error($this->digitalmapModel->getError());
            }
            $r = $this->digitalmapModel->save($data);

            if ($r) {
                $this->success('提交成功！', cookie('__forward__'));
            } else {
                $this->error('提交失败！');
            }
        } else {
            $id =  I('id',0, 'intval');
            if (!$id) {
                $this->error('缺少必要的参数！');
            }
            $data = $this->digitalmapModel->find($id);
            $data['config'] = json_decode($data['config'], true);
            $this->assign('map', $data);
            $this->display();
        }
    }

    //新增地图
    public function add()
    {
        if (IS_POST) {
            $data = $_POST;
            $baidumap['bdmap_x'] = $_POST['bdmap_x'];
            $baidumap['bdmap_y'] = $_POST['bdmap_y'];
            $baidumap['name'] = $_POST['name'];
            $baidumap['address'] = $_POST['address'];
            $baidumap['tel'] = $_POST['tel'];
            $data['config'] = json_encode($baidumap);

            if (false === $this->digitalmapModel->create()) {
                $this->error($this->digitalmapModel->getError());
            }
            $r = $this->digitalmapModel->add($data);

            if ($r) {
                $this->success('提交成功！', cookie('__forward__'));
            } else {
                $this->error('提交失败！');
            }
        } else {
            $this->display();
        }
    }

    //删除电子地图
    function delete()
    {
        $id = I('get.id', 0, 'intval');

        if (!$id) {
            $this->error('缺少必要的参数!');
        }

        if (false !== $this->digitalmapModel->delete($id)) {
            $this->success('删除失败！');
        } else {
            $this->error('删除失败！: ' . $this->digitalmapModel->getDbError());
        }
    }
}